#include <iostream>
#include <vector>

using namespace std;
class Solution
{
public:
    /**
     * @brief time out
     *  需要标记，避免重复访问
     * 1.设置访问过的元素为INT_MAX ,之后不再访问
     * 2.交换访问过的元素和index
     * @param nums 
     * @return int 
     */
    int arrayNesting(vector<int> &nums)
    {
        int ans = 0, n = nums.size();
        for (int i = 0; i < n; i++)
        {
            int temp = 1, index = i;
            while (nums[index] != i)
            {
                index = nums[index];
                cout << index << " ";
                temp++;
            }
            cout << endl;
            ans = max(temp, ans);
        }
        return ans;
    }
};
int main()
{
    Solution s;
    vector<int> nums = {5, 4, 0, 3, 1, 6, 2};
    cout << s.arrayNesting(nums) << endl;
    system("pause");
    return 0;
}
